package com.unionx.unionxuf.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {

	  private static final String[] IGNORE_URI = {"/home2.jsp", "/login"};
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		
		String requestUrls = request.getRequestURI();
		System.out.println(requestUrls+"$$");
		 String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");
		 System.out.println(requestUrl+"###");
	            for(String url : IGNORE_URI) {    
	                if(requestUrl.contains(url)) {    
	                    return true;    
	                }    
	            } 
		    String  username = (String) request.getSession().getAttribute("name");  
	        if(username != null) {    
	            return true;  //返回true，则这个方面调用后会接着调用postHandle(),  afterCompletion()  
	        }else{
	        	response.sendRedirect("localhost:8080/unionx_uf/views/home2.jsp");
	        }
			return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
